// pages/my/info/info.js
Page({
  data: {
    avatar: [],
    avatarId: "",
    nickName: "",
    region: "",
    ageRange: {},
    job: {},
    wantTask: "1",
    wxNumbers: {},
    qqNumbers: {},

    taskList: [],
    currentTaskTmp: {},
    currentTask: {
      id: "1",
      name: "全部",
    },
    wechatPic: [{
      url: "https://www.linxiaobang.com.cn/miniprogram/image/wxfriend.jpg",
    }],
    qqPic: [{
      url: "https://www.linxiaobang.com.cn/miniprogram/image/qqfriend.jpg"
    }],

    wechatFriendPic: [],
    wechatFriendPicId: "",
    qqFriendPic: [],
    qqFriendPicId: "",
    isDialog_completeInfo: true,
    ageList_show: false,
    ageList: [],
    jobList_show: false,
    jobList: [],
    WCList_show: false,
    WCList: [],
    QQList_show: false,
    QQList: [],
    isDialog_choosetask: false,
    beforeClose: null
  },
  onLoad: function (options) {
    this.getUserInfo();
    this.setData({
      ageList: wx.getStorageSync("ageList"),
      jobList: wx.getStorageSync("jobList"),
      taskList: wx.getStorageSync("taskList"),
    });
  },
  onShow: function () {
    let city = wx.getStorageSync("city_select");
    if (!!city) {
      this.setData({
        region: wx.getStorageSync("city_select")
      });
    }
  },
  getUserInfo() {
    getApp().globalData.http.userInfo().then(res => {
      let info = res.data;
      this.setData({
        avatar: [{
          url: info.avatar
        }],
        avatarId: info.avatarId,
        nickName: info.nickName,
        region: info.region,
        ageRange: info.ageRange,
        job: info.job,
        wantTask: !!info.wantTask ? info.wantTask.id : 0,
        currentTask: info.wantTask,
        wxNumbers: info.wxNumbers,
        qqNumbers: info.qqNumbers,
        wechatFriendPic: !info.wxFriend ? [] : Img[{
          url: info.wxFriendImg
        }],
        qqFriendPic: !info.qqFriendImg ? [] : [{
          url: info.qqFriendImg
        }],
        wechatFriendPicId: info.wxFriendImgId,
        qqFriendPicId: info.qqFriendImgId
      });
      wx.setStorageSync("userInfo", res.data);
    });
  },
  avatarUpload() {
    let _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: "compressed",
      success(res) {
        let filePath = res.tempFilePaths[0];
        _this.setData({
          avatar: [{
            url: filePath,
            status: "uploading",
            message: "上传中"
          }]
        });
        getApp().globalData.http.upload("name", filePath, {
          srcType: 1
        }).then(_res => {
          let tmp = _this.data.avatar;
          tmp[0].status = "success";
          _this.setData({
            avatar: tmp,
            avatarId: _res.data.id
          });
        });
      }
    })
  },
  taskHandle() {
    this.setData({
      currentTaskTmp: this.data.currentTask,
      isDialog_choosetask: true
    });
  },
  taskSel(e) {
    let tmp = {
      id: e.currentTarget.dataset.obj.id,
      name: e.currentTarget.dataset.obj.name,
    }
    this.setData({
      currentTaskTmp: tmp
    });
  },
  taskConfirm() {
    this.setData({
      currentTask: this.data.currentTaskTmp,
      isDialog_choosetask: false
    });
  },
  completeInfo() {
    this.setData({
      isDialog_completeInfo: true
    });
  },
  cityHandle() {
    wx.navigateTo({
      url: "/pages/morejob/cityselect/cityselect",
    })
  },
  actionHandle(e) {
    let prop = e.currentTarget.dataset.showname;
    this.setData({
      [prop]: true
    });
  },
  actionClose(e) {
    let prop = e.currentTarget.dataset.showname;
    this.setData({
      [prop]: false
    });
  },
  actionSel(e) {
    let prop = e.currentTarget.dataset.showname;
    let type = e.currentTarget.dataset.type;
    this.setData({
      [prop]: false,
      [type]: e.detail
    });
  },
  submit() {
    let params = {
      avatar: this.data.avatar[0].url,
      avatarId: this.data.avatarId,
      nickName: this.data.nickName,
      region: this.data.region,
      ageRange: !this.data.ageRange ? "" : this.data.ageRange.id,
      job: !this.data.job ? "" : this.data.job.id,
      wantTask: this.data.wantTask.length == 0 ? "" : this.data.currentTask.id,
      wxNumbers: !this.data.wxNumbers ? "" : this.data.wxNumbers.id,
      qqNumbers: !this.data.qqNumbers ? "" : this.data.qqNumbers.id,
      wxFriendImgId: this.data.wechatFriendPicId,
      qqFriendImgId: this.data.qqFriendPicId
    };
    console.log(params)
    if (!params.nickName) {
      getApp().globalData.vant.toast("请输入昵称");
    } else if (params.nickName.length > 10) {
      getApp().globalData.vant.toast("昵称不能超过10个字");
    } else {
      getApp().globalData.http.updateInfo(params).then(res => {
        getApp().globalData.vant.toast({
          type: "success",
          message: res.msg,
          duration: 1000,
          onClose: () => {
            wx.switchTab({
              url: "/pages/my/index/index",
            });
            getCurrentPages()[0].onLoad();
          },
        });
      });
    }
  },
  uploadWXFri(e) {
    let filePath = e.detail.file.url;
    this.setData({
      wechatFriendPic: [{
        url: filePath,
        status: "uploading",
        message: "上传中"
      }]
    });
    getApp().globalData.http.upload("name", filePath, {
      srcType: 1
    }).then(res => {
      let tmp = this.data.wechatFriendPic;
      tmp[0].status = "success";
      this.setData({
        wechatFriendPic: tmp,
        wechatFriendPicId: res.data.id
      });
    });
  },
  deleteWXFri() {
    this.setData({
      wechatFriendPic: [],
    });
  },
  uploadQQFri(e) {
    let filePath = e.detail.file.url;
    this.setData({
      qqFriendPic: [{
        url: filePath,
        status: "uploading",
        message: "上传中"
      }]
    });
    getApp().globalData.http.upload("name", filePath, {
      srcType: 1
    }).then(res => {
      let tmp = this.data.qqFriendPic;
      tmp[0].status = "success";
      this.setData({
        qqFriendPic: tmp,
        qqFriendPicId: res.data.id
      });
    });
  },
  deleteQQFri() {
    this.setData({
      wechatFriendPic: [],
    });
  },
})